package org.karelun.gef;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.ui.actions.ActionBarContributor;
import org.eclipse.gef.ui.actions.AlignmentRetargetAction;
import org.eclipse.gef.ui.actions.DeleteRetargetAction;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.gef.ui.actions.MatchHeightRetargetAction;
import org.eclipse.gef.ui.actions.MatchWidthRetargetAction;
import org.eclipse.gef.ui.actions.RedoRetargetAction;
import org.eclipse.gef.ui.actions.UndoRetargetAction;
import org.eclipse.gef.ui.actions.ZoomInRetargetAction;
import org.eclipse.gef.ui.actions.ZoomOutRetargetAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.karelun.gef.actions.EditorActionFactory;
import org.karelun.gef.helper.ImageResource;

public class DiagramActionBarContributor extends ActionBarContributor {

	protected void buildActions() {
		addRetargetAction(new UndoRetargetAction());
		addRetargetAction(new RedoRetargetAction());
		addRetargetAction(new DeleteRetargetAction());
		addRetargetAction(new ZoomInRetargetAction());
		addRetargetAction(new ZoomOutRetargetAction());

		RetargetAction cutAction = new RetargetAction(
				ActionFactory.CUT.getId(), "Cut"); //$NON-NLS-1$
		cutAction.setImageDescriptor(ImageResource
				.getImageDesc(ImageResource.CUT));
		addRetargetAction(cutAction);

		RetargetAction copyACtion = new RetargetAction(ActionFactory.COPY
				.getId(), "Copy"); //$NON-NLS-1$
		copyACtion.setImageDescriptor(ImageResource
				.getImageDesc(ImageResource.COPY));
		addRetargetAction(copyACtion);

		RetargetAction pasteACtion = new RetargetAction(ActionFactory.PASTE
				.getId(), "Paste"); //$NON-NLS-1$
		pasteACtion.setImageDescriptor(ImageResource
				.getImageDesc(ImageResource.PASTE));
		addRetargetAction(pasteACtion);
		RetargetAction selectAllAction = new RetargetAction(
				ActionFactory.SELECT_ALL.getId(), "SelectAll"); //$NON-NLS-1$
		addRetargetAction(selectAllAction);

		addRetargetAction(new AlignmentRetargetAction(PositionConstants.LEFT));
		addRetargetAction(new AlignmentRetargetAction(PositionConstants.CENTER));
		addRetargetAction(new AlignmentRetargetAction(PositionConstants.RIGHT));
		addRetargetAction(new AlignmentRetargetAction(PositionConstants.TOP));
		addRetargetAction(new AlignmentRetargetAction(PositionConstants.MIDDLE));
		addRetargetAction(new AlignmentRetargetAction(PositionConstants.BOTTOM));

		RetargetAction action = new RetargetAction(
				GEFActionConstants.TOGGLE_GRID_VISIBILITY, "Grid", //$NON-NLS-1$
				IAction.AS_CHECK_BOX);
		action.setImageDescriptor(ImageResource
				.getImageDesc(ImageResource.GRID));
		addRetargetAction(action);

		addRetargetAction(new MatchHeightRetargetAction());
		addRetargetAction(new MatchWidthRetargetAction());

		action = new RetargetAction(EditorActionFactory.ADDATTRIBUTE.getId(),
				"Add Attribute"); //$NON-NLS-1$
		action.setImageDescriptor(ImageResource
				.getImageDesc(ImageResource.ATTRIBUTE));
		addRetargetAction(action);
		action = new RetargetAction(EditorActionFactory.ADDMETHOD.getId(),
				"Add Method"); //$NON-NLS-1$
		action.setImageDescriptor(ImageResource
				.getImageDesc(ImageResource.METHOD));
		addRetargetAction(action);
	}

	@Override
	protected void declareGlobalActionKeys() {
		addGlobalActionKey(ActionFactory.CUT.getId());
		addGlobalActionKey(ActionFactory.COPY.getId());
		addGlobalActionKey(ActionFactory.PASTE.getId());
		addGlobalActionKey(ActionFactory.UNDO.getId());
		addGlobalActionKey(ActionFactory.REDO.getId());
	}

	public void contributeToToolBar(IToolBarManager toolBar) {
		toolBar.add(new Separator());
		toolBar.add(getActionRegistry().getAction(ActionFactory.UNDO.getId()));
		toolBar.add(getActionRegistry().getAction(ActionFactory.REDO.getId()));
		toolBar.add(new Separator());

		toolBar.add(getActionRegistry().getAction(ActionFactory.CUT.getId()));
		toolBar.add(getActionRegistry().getAction(ActionFactory.COPY.getId()));
		toolBar.add(getActionRegistry().getAction(ActionFactory.PASTE.getId()));
		toolBar
				.add(getActionRegistry()
						.getAction(ActionFactory.DELETE.getId()));
		super.contributeToToolBar(toolBar);

		toolBar.add(new Separator());
		toolBar.add(getActionRegistry().getAction(
				EditorActionFactory.ADDATTRIBUTE.getId()));
		toolBar.add(getActionRegistry().getAction(
				EditorActionFactory.ADDMETHOD.getId()));

		toolBar.add(new Separator());
		toolBar.add(getActionRegistry()
				.getAction(GEFActionConstants.ALIGN_LEFT));
		toolBar.add(getActionRegistry().getAction(
				GEFActionConstants.ALIGN_CENTER));
		toolBar.add(getActionRegistry().getAction(
				GEFActionConstants.ALIGN_RIGHT));

		toolBar.add(new Separator());
		toolBar
				.add(getActionRegistry()
						.getAction(GEFActionConstants.ALIGN_TOP));
		toolBar.add(getActionRegistry().getAction(
				GEFActionConstants.ALIGN_MIDDLE));
		toolBar.add(getActionRegistry().getAction(
				GEFActionConstants.ALIGN_BOTTOM));

		toolBar.add(new Separator());
		toolBar.add(getActionRegistry().getAction(
				GEFActionConstants.MATCH_HEIGHT));
		toolBar.add(getActionRegistry().getAction(
				GEFActionConstants.MATCH_WIDTH));

		toolBar.add(new Separator());
		toolBar.add(getActionRegistry().getAction(GEFActionConstants.ZOOM_IN));
		toolBar.add(getActionRegistry().getAction(GEFActionConstants.ZOOM_OUT));

		toolBar.add(new Separator());
		toolBar.add(getAction(GEFActionConstants.TOGGLE_GRID_VISIBILITY));

	}
}
